<?php
/**
 * 文章相关接口
 * User: cxm
 * Date: 2020/12/4
 * Time: 17:12
 */
namespace App\Http\Controllers\Home;
use App\Models\Article;
use Illuminate\Http\Request;

class ArticleController extends Controller {
    /**
     * 文章列表
     * URL POST:/api/home/article/getLists
     * @param Request $request
     * @param int page 页码
     * @param int limit 每页显示条数
     * @return mixed
     */
    public function getLists(Request $request) {
        $params = $request->only(['page','limit']);
        // 排序
        $params['orderBy'] = ['created_at desc'];
        // 显示
        $params['where'][] = ['status',1];
        // 图片
        $params['with'][] = 'file:id,path';
        $result = Article::listByWhere($params);
        return $this->success('查询成功',$result);
    }

    /**
     * 文章详情
     * URL POST:/api/home/article/show
     * @param Request $request
     * @param int id 文章ID
     * @return mixed
     */
    public function show(Request $request){
        $params = $request->only(['id']);
        if (!array_key_exists('id',$params) || empty($params['id'])) {
            return $this->error('文章ID是必须的');
        }
        $params['where'][] = ['id',$params['id']];
        // 图片
        $params['with'][] = 'file:id,path';
        $info = Article::detailsWith($params);
        return $this->success('成功',$info);
    }
}